package com.samplecompany.gui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

public class MashupPanel extends Panel implements MashupContainer {
	/** */
	private static final long serialVersionUID = 1L;

	private final List<Component> children = new ArrayList<Component>();
	 
	private LinkedList<Component> previouslyRenderedChildren;

	private final MashupListView mashupListView;
	
	public static class MashupListView extends ListView<Component> {
		/** */
		private static final long serialVersionUID = 1L;

		public MashupListView(String id, List<Component> children) {
			super(id, children);
			setReuseItems(true);
		}

		/**
		 * @see org.apache.wicket.markup.html.list.ListView#populateItem(org.apache.wicket.markup.html.list.ListItem)
		 */
		@Override
		protected void populateItem(ListItem<Component> listItem) {
			Component component = listItem.getModelObject();
			if (!component.getId().equals(GID)) {
				throw new IllegalStateException("Currently handles only children with id " + GID);
			}
			listItem.add(component);
		}
	}

	public MashupPanel() {
		this(GID);
	}
	
	/**
	 * @param id
	 */
	public MashupPanel(String id) {
		super(id);
		super.add(mashupListView = newMashupListView());
	}

	/**
	 * @return MashUpListView
	 */
	protected MashupListView newMashupListView() {
		return new MashupListView("listview-id", children);
	}
	
	/**
	 * @see org.apache.wicket.Component#onBeforeRender()
	 */
	@Override
	protected void onBeforeRender() {
		if (isChanged()) {
			mashupListView.removeAll();
		}
		
		previouslyRenderedChildren = new LinkedList<Component>(children);
		
		super.onBeforeRender();
	}
	
	/**
	 * @see com.samplecompany.gui.MashUpContainer#isChanged()
	 */
	public boolean isChanged() {
		if (previouslyRenderedChildren != null) {
			return !((children.size() == previouslyRenderedChildren.size()) && previouslyRenderedChildren.containsAll(children));
		}
		
		return false;
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#add(org.apache.wicket.Component)
	 */
	public boolean add(Component component) {
		return children.add(component);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#add(int, org.apache.wicket.Component)
	 */
	public void add(int index, Component element) {
		children.add(index, element);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#addAll(java.util.Collection)
	 */
	public boolean addAll(Collection<? extends Component> collection) {
		return children.addAll(collection);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#addAll(int, java.util.Collection)
	 */
	public boolean addAll(int index, Collection<? extends Component> collection) {
		return children.addAll(index, collection);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#clear()
	 */
	public void clear() {
		children.clear();
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#contains(java.lang.Object)
	 */
	public boolean contains(Object o) {
		return children.contains(o);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#containsAll(java.util.Collection)
	 */
	public boolean containsAll(Collection<?> collection) {
		return children.containsAll(collection);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#indexOf(java.lang.Object)
	 */
	public int indexOf(Object o) {
		return children.indexOf(o);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#isEmpty()
	 */
	public boolean isEmpty() {
		return children.isEmpty();
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#lastIndexOf(java.lang.Object)
	 */
	public int lastIndexOf(Object o) {
		return children.lastIndexOf(o);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#remove(java.lang.Object)
	 */
	public boolean remove(Object o) {
		return children.remove(o);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#remove(int)
	 */
	public Component remove(int index) {
		return children.remove(index);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#removeAll(java.util.Collection)
	 */
	public boolean removeAll(Collection<?> collection) {
		return children.removeAll(collection);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#retainAll(java.util.Collection)
	 */
	public boolean retainAll(Collection<?> collection) {
		return children.retainAll(collection);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#set(int, org.apache.wicket.Component)
	 */
	public Component set(int index, Component element) {
		if (children.size() <= index) {
			for (int subIndex = children.size(); subIndex <= index; subIndex++) {
				if (subIndex != index) {
					add(subIndex, new WebMarkupContainer(GID));
				} else {
					add(subIndex, element);
				}
			}
			
			return null;
		}
		
		return children.set(index, element);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#subList(int, int)
	 */
	public List<Component> subList(int fromIndex, int toIndex) {
		return children.subList(fromIndex, toIndex);
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#toArray()
	 */
	public Object[] toArray() {
		return children.toArray();
	}

	/**
	 * @see com.samplecompany.gui.MashUpContainer#toArray(T[])
	 */
	public <T> T[] toArray(T[] a) {
		return children.toArray(a);
	}

	public MashupListView getMashUpListView() {
		return mashupListView;
	}
}
